package com.example.web_task.controller;


import com.example.web_task.constant.ExceptionConstants;
import com.example.web_task.entity.College;
import com.example.web_task.entity.Result;
import com.example.web_task.exception.CollegeNotExistException;
import com.example.web_task.exception.ParameterException;
import com.example.web_task.service.ICollegeService;
import com.example.web_task.vo.CollegeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学院 前端控制器
 * </p>
 *
 * @author suheng
 * @since 2024-02-23
 */
@RestController
@RequestMapping("/college")
@Api(tags = "学院相关接口")
@Slf4j
public class CollegeController {
    @Autowired
    private ICollegeService iCollegeService;
    /*
    新增一个学院
     */
    @PostMapping
    @ApiOperation("新增一个学院")
    public Result<String> insert(@RequestBody College college){
        log.info("新增一个学院");
        if(college==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        iCollegeService.save(college);
        return Result.success();
    }
    /*
    根据id删除一个学院
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除一个学院")
    public Result<String>delete(@PathVariable Long id){
        log.info("删除一个学院 id="+id);
        if(id==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        boolean isSuccess=iCollegeService.removeById(id);
        if(!isSuccess){
            throw new CollegeNotExistException(ExceptionConstants.COLLEGE_NOT_EXIST);
        }
        return Result.success();
    }
    /*
    根据id查询一个学院信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询一个学院信息")
    public Result<CollegeVo>getById(@PathVariable Long id){
        log.info("根据id查询一个学院信息，id="+id);
        if(id==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        CollegeVo collegeVo=iCollegeService.getMessageById(id);
        if(collegeVo==null){
            throw new CollegeNotExistException(ExceptionConstants.COLLEGE_NOT_EXIST);
        }
        return Result.success(collegeVo);
    }
    /*
    根据id修改学院信息
     */
    @PutMapping
    @ApiOperation("更新学院信息")
    public Result<String>update(@RequestBody College college){
        if(college==null||college.getId()==null){
            throw new ParameterException(ExceptionConstants.PARAMMETER_ERROR);
        }
        boolean isSuccess = iCollegeService.updateById(college);
        if(!isSuccess){
            throw new CollegeNotExistException(ExceptionConstants.COLLEGE_NOT_EXIST);
        }
        return Result.success();
    }

}
